package com.deer.wms.busine.tactic.web;

import com.deer.wms.busine.tactic.model.Batch.BatchTacticDetail;
import com.deer.wms.busine.tactic.model.Batch.BatchTacticDetailCriteria;
import com.deer.wms.busine.tactic.model.Batch.BatchTacticDetailDto;
import com.deer.wms.busine.tactic.service.BatchTacticDetailService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2019/11/29.
*/
@Api(description = "批次详细信息api接口")
@RestController
@RequestMapping("/batch/tactic/details")
public class BatchTacticDetailController {

    @Autowired
    private BatchTacticDetailService batchTacticDetailService;

    @PostMapping("/insert")
    public Result add(@RequestBody BatchTacticDetail batchTacticDetail) {
        batchTacticDetailService.save(batchTacticDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        batchTacticDetailService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/deleteByCode")
    public Result deleteByCode(String batchTacticCode) {
        batchTacticDetailService.deleteByCode(batchTacticCode);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody BatchTacticDetail batchTacticDetail) {
        batchTacticDetailService.updateById(batchTacticDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        BatchTacticDetail batchTacticDetail = batchTacticDetailService.getById(id);
        return ResultGenerator.genSuccessResult(batchTacticDetail);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @Transactional
    @GetMapping("/list")
    public Result list(BatchTacticDetailCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<BatchTacticDetailDto> list = batchTacticDetailService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "根据状态查list", notes = "根据状态查list")
    @GetMapping("/StateList")
    public Result findByState(Integer state, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        List<BatchTacticDetailDto> list = batchTacticDetailService.findByState(state);
        return ResultGenerator.genSuccessResult(list);
    }

}
